package com.example.b_308.myapplication.view.main;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.b_308.myapplication.R;
import com.example.b_308.myapplication.view.base.BaseActivity;
import com.example.b_308.myapplication.view.base.BaseFragment;
import com.example.b_308.myapplication.view.page.WorkPageAdapter;
import com.example.b_308.myapplication.view.page.WorkViewPage;
import com.example.b_308.myapplication.view.title.TitleBar;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    WorkViewPage w_vp;
    RadioGroup rg;
    TitleBar titleBar;
    List<BaseFragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (null != getSupportActionBar()) {
            getSupportActionBar().hide();
        }
        setContentView(R.layout.activity_main);

        w_vp = (WorkViewPage) findViewById(R.id.work_view_page);
        rg = (RadioGroup) findViewById(R.id.rg_bottom_tab);

        titleBar = (TitleBar)findViewById(R.id.title_bar);
        titleBar.setTitle("企聊");
        titleBar.setIvReturn(R.color.black);

        fragments.add(new MessageFragment());
        fragments.add(new AddressFragment());
        fragments.add(new WorkFragment());
        fragments.add(new MineFragment());

        w_vp.setAdapter(new WorkPageAdapter(getSupportFragmentManager(), fragments));
        w_vp.setCurrentItem(0);
        ((RadioButton) rg.getChildAt(0)).setChecked(true);
        w_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //当界面滑动的时候
            }

            @Override
            public void onPageSelected(int position) {
                //当界面选中的时候
                ((RadioButton) rg.getChildAt(position)).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //当界面滑动状态改变的时候
            }
        });
    }

    public void check(View view) {
       int index= rg.indexOfChild(view);
        w_vp.setCurrentItem(index);
        int current =index;
        switch (current){
            case 0:
                titleBar.setTitle("企聊");
                titleBar.setIvReturn(R.color.black);
                break;
            case 1:
                titleBar.setTitle("通讯录");
                titleBar.setIvReturn(R.color.black);
                break;
            case 2:
                titleBar.setTitle("工作台");
                titleBar.setIvReturn(R.color.black);
                break;
            case 3:
                titleBar.setTitle("我的");
                titleBar.setIvReturn(R.color.black);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        if(requestCode==?){
//            if (resultCode==?)
//        }
//        (setResult();)
    }
}

